package com.tongfusoft.logistics.service.impl.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

import com.tongfusoft.da.help.DBUtils;
import com.tongfusoft.utils.CryptoUtils;
import com.tongfusoft.utils.MapUtils;
import com.tongfusoft.web.core.data.PrivilegeData;
import com.tongfusoft.web.core.data.ResourceData;
import com.tongfusoft.web.core.data.User;
import com.tongfusoft.web.core.service.ISecurityService;

public class SecurityServiceImpl implements ISecurityService{

	@Override
	public Collection<GrantedAuthority> loadUserAuthoritiesByName(String userName) {
		return Collections.EMPTY_LIST;
	}

	@Override
	public String getPwdByName(String userName) {
		List<Map<String,String>> list = DBUtils.executeSelect("/security#memberPwdByName", MapUtils.factory().put("memberName", userName).create());
		if(list != null && list.size() > 0){
			return list.get(0).get("MEMBER_PWD");
		}
		return null;
	}

	@Override
	public User getUserInfo(String userName) {
		List<User> list = DBUtils.executeSelect("/security#memberInfoByName", MapUtils.factory().put("memberName", userName).create());
		if(list != null && list.size() == 1){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<PrivilegeData> getAllPrivilege() {
		return Collections.EMPTY_LIST;
	}

	@Override
	public List<ResourceData> getResroucesByPrivilege(PrivilegeData privilege) {
		return Collections.EMPTY_LIST;
	}

	@Override
	public boolean validateUser(Map params) {
		if(StringUtils.equals(CryptoUtils.getMD5((String)params.get("j_password")), this.getPwdByName((String)params.get("j_username")))){
			return true;
		}
		return false;
	}

	@Override
	public Map<String, Object> getPredefineContent(User user, Map params) {
		Map<String,Object> map = new HashMap<String,Object>();
		return map;
	}

}
